////////////////////////////////////////////////////////////////////////////////
//
//  FLYING KIWI BV
//  Copyright 2010 Flying Kiwi BV
//  All Rights Reserved.
//
//  NOTICE: Flying Kiwi permits you to use, modify, and distribute this file
//  in accordance with the terms of the license agreement accompanying it.
//
////////////////////////////////////////////////////////////////////////////////
package plantool.modules.workplaces.display {
	//--------------------------------------------------------------------------
	//  Imports
	//--------------------------------------------------------------------------
	import flash.events.Event;
	
	import kiwi.components.DragObject;
	import kiwi.components.IDropTarget;
	import kiwi.utils.Validation;
	
	import plantool.data.QueryErrorResult;
	import plantool.data.QueryResult;
	import plantool.modules.users.data.User;
	import plantool.modules.users.data.UserHourData;
	import plantool.modules.users.data.UserHourTypes;
	import plantool.modules.users.display.IUserDay;
	
	//--------------------------------------------------------------------------
	//  [Metadata (Events, Style, Effects etc)]
	//--------------------------------------------------------------------------
	[Event(name="change", type="flash.events.Event")]
	
	//--------------------------------------------------------------------------
	//  Class declaration
	//--------------------------------------------------------------------------
	public class WorkplaceUserHour extends WorkplaceDisplayObject implements IWorkplaceUserHour {
		//--------------------------------------------------------------------------
		//Class constants
		//--------------------------------------------------------------------------
		
		//--------------------------------------------------------------------------
		//Class variables
		//--------------------------------------------------------------------------
		private var _date:Date				= null;
		private var _hourData:UserHourData 	= null;
		private var _hotspot:DragObject		= null;
		private var _user:User				= null;
		private var _skin:WorkplaceUserHourSkin = null;
		
		//--------------------------------------------------------------------------
		//Class properties
		//--------------------------------------------------------------------------
		public function get date():Date {
			return _date;
		}
		public function set date(value:Date):void {
			_date = new Date(value);
		}
		
		public function get hourData():UserHourData {
			return _hourData;
		}
		public function set hourData(value:UserHourData):void {
			//store hour data
			_hourData = value;
			
			//redraw button
			invalidate();
		}
		
		public function get user():User {
			return _user;
		}
		
		public function set user(value:User):void {
			_user = value;
			
			//redraw button
			invalidate();
		}
		
		//--------------------------------------------------------------------------
		//Constructor and Destructor
		//--------------------------------------------------------------------------
		public function WorkplaceUserHour() {
			super();
		}
		
		public override function dispose():void {
			//dispose date
			if (Validation.isSet(_date)) {
				_date = null;
			}
			
			//dispose hourdata
			if (Validation.isSet(_hourData)) {
				_hourData = null;
			}
			
			//dispose hotspot
			if (Validation.isSet(_hotspot)) {
				//dispose hotspot
				_hotspot.dispose();
				_hotspot = null;
			}
			
			if (Validation.isSet(_skin)) {
				_skin = null;
			}
			
			//dispatch event so parent knows we're disposed
			dispatchEvent(new Event(Event.CHANGE));
			
			//dispose object
			super.dispose();
		}
		
		//--------------------------------------------------------------------------
		//Class methods
		//--------------------------------------------------------------------------
		private function onComplete_removeHour(result:QueryResult):void {
		}
		
		private function onDrop_hour(target:IDropTarget, item:DragObject):void {
			//validate target
			if (target is IUserDay) {
				//validate hourdata
				if (Validation.isSet(_hourData)) {
					userApi.removeUserHours([_hourData], onComplete_removeHour, onError_createHour);
				}
			} else if (target is IWorkplaceDay) {
				//validate hourdata
				if (Validation.isSet(_hourData)) {
					_hourData.related_id = (target as IWorkplaceDay).workplace.id;
					userApi.updateUserHour(_hourData, onComplete_removeHour, onError_createHour);
				}
			}
		}
		
		private function onValidateDrop_hour(target:IDropTarget, item:DragObject):Boolean {
			if (target is IUserDay) {
				//cast day
				var userDay:IUserDay = target as IUserDay;
				
				//validate day
				return (dateApi.validateDay(date, userDay.date) && userDay.user.id == user.id);
			} else if (target is IWorkplaceDay) {
				//cast day
				var workplaceDay:IWorkplaceDay = target as IWorkplaceDay;
				
				//validate day
				if (workplaceDay.workplace.id == workplace.id) {
					return false;
				} else {
					return dateApi.validateDay(date, workplaceDay.date);
				}
			}
			
			return false;
		}
		
		private function onError_createHour(result:QueryErrorResult):void {
			trace("Error", result.query);
		}
		
		protected override function init():void {
			//create hotspot
			_skin				= new WorkplaceUserHourSkin();
			_skin.color			= _user.color;
			
			_hotspot			= new DragObject();
			_hotspot.skin		= _skin;
			_hotspot.width 		= 20;
			_hotspot.height		= 10;
			
			//drag properties
			_hotspot.dragItem		= this;
			_hotspot.droppedHandler			= onDrop_hour;
			_hotspot.validateDropHandler	= onValidateDrop_hour;
			_hotspot.dropTargets.push(IUserDay);
			_hotspot.dropTargets.push(IWorkplaceDay);
			
			addChild(_hotspot);
			
			//draw hour
			invalidate();
		}
	}
}